package com.spring.pick2pick;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spring.pick2pick.common.PageHandler;
import com.spring.pick2pick.common.Util;
import com.spring.pick2pick.dao.NoticeDao;
import com.spring.pick2pick.model.AdminItem;
import com.spring.pick2pick.model.NoticeItem;

@Controller
public class NoticeController {
	
	@Autowired
	NoticeDao noticeDao;
	
	/** 공지사항 리스트 목록 */
	@RequestMapping(value = "listNotice", method = RequestMethod.GET)
	public String listNotice(Locale locale, Model model, HttpServletRequest request) {
		int page = 1;
		int size = 10;
		String pageStr = request.getParameter("page");
		
		if (pageStr != null) {
			page = Integer.parseInt(pageStr);
		}
		
		model.addAttribute("list", noticeDao.getNoticeList(page, size));
		model.addAttribute("page", PageHandler.getInstance());
		
		return "notice/listNotice";
	}
	
	/** 공지사항 추가 */
	@RequestMapping(value = "addNotice", method = RequestMethod.GET)
	public String addAdmin(Locale locale, Model model) {
		return "notice/addNotice";
	}
	
	/** 공지사항 추가후 리스트 목록 */
	@RequestMapping(value = "addNoticeOk", method = RequestMethod.POST)
	public String addAdminOk(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		String noticeTitle = request.getParameter("noticeTitle");
		String noticeText = request.getParameter("noticeText");
		
		if (noticeTitle == null || noticeText == null || noticeTitle.equals("") || noticeText.equals("")) {
			Util.getInstance().alertPage("필수입력 항목이 누락되었습니다.", response);
		} else {
			NoticeItem item = new NoticeItem();
			item.setNoticeTitle(noticeTitle);
			item.setNoticeText(noticeText);
			
			HttpSession session = request.getSession();
			AdminItem loginInfo = (AdminItem) session.getAttribute("ADMIN_INFO");
			item.setIdAdmin(loginInfo.getIdAdmin());
			
			int result = noticeDao.addNotice(item);
			
			if (result > 0) {
				try {
					response.sendRedirect("listNotice");
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				Util.getInstance().alertPage("추가에 실패하였습니다.", response);
			}
		}
		return null;
	}
	
	/** 공지사항 전시여부 변경 */
	@RequestMapping(value = "displayNotice", method = RequestMethod.GET)
	public String displayNotice(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		String pageStr = request.getParameter("page");
		String idStr = request.getParameter("id");
		String isDisplayStr = request.getParameter("isDisplay");
		
		if (pageStr == null || idStr == null || isDisplayStr == null || pageStr.equals("") || idStr.equals("") || isDisplayStr.equals("")) {
			Util.getInstance().alertPage("필수입력 항목이 누락되었습니다.", response);
		} else {
			int page = Integer.parseInt(pageStr);
			int id = Integer.parseInt(idStr);
			int isDisplay = Integer.parseInt(isDisplayStr);
			
			if (isDisplay == 0) {
				isDisplay = 1;
			} else if (isDisplay == 1) {
				isDisplay = 0;
			}
			
			NoticeItem item = new NoticeItem();
			item.setIdNotice(id);
			item.setIsDisplay(isDisplay);
			int result = noticeDao.editDisplay(item);
			
			if (result > 0) {
				
				String html = "<!doctype html>";
				html += "<head>";
				html += "<meta charset='utf-8'>";
				html += "<script>";
				html += "alert('정상적으로 수정되었습니다.');";
				html += "location.replace('listNotice?page=" + page + "');";
				html += "</script>";
				html += "</head>";
				html += "</html>";
				
				try {
					response.setCharacterEncoding("utf-8");
					PrintWriter out = response.getWriter();
					out.print(html);
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				Util.getInstance().alertPage("전시여부 수정에 실패하였습니다.", response);
			}
			
		}
		
		return null;
	}
	
	/** 공지사항 조회 */
	@RequestMapping(value = "viewNotice", method = RequestMethod.GET)
	public String viewAdmin(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		String idStr = request.getParameter("id");
		String pageStr = request.getParameter("page");
		
		if (idStr == null || idStr.equals("") || pageStr == null || pageStr.equals("")) {
			Util.getInstance().alertPage("필수입력 항목이 누락되었습니다.", response);
		} else {
			int id = Integer.parseInt(idStr);
			int page = Integer.parseInt(pageStr);
			
			NoticeItem item = new NoticeItem();
			item.setIdNotice(id);
			
			NoticeItem result = noticeDao.getNotice(item);
			model.addAttribute("item", result);
			model.addAttribute("page", page);
		}
		
		return "notice/viewNotice";
	}
	
	/** 공지사항 수정후 리스트 목록 */
	@RequestMapping(value = "editNoticeOk", method = RequestMethod.POST)
	public String editNoticeOk(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		String noticeTitle = request.getParameter("noticeTitle");
		String noticeText = request.getParameter("noticeText");
		String idNoticeStr = request.getParameter("idNotice");
		String isDisplayStr = request.getParameter("isDisplay");
		String pageStr = request.getParameter("page");
		
		if (noticeTitle == null || noticeText == null || idNoticeStr == null || pageStr == null || isDisplayStr == null ||
				noticeTitle.equals("") || noticeText.equals("") || idNoticeStr.equals("") || pageStr.equals("") || isDisplayStr.equals("")) {
			Util.getInstance().alertPage("필수입력 항목이 누락되었습니다.", response);
		} else {
			int idNotice = Integer.parseInt(idNoticeStr);
			int isDisplay = Integer.parseInt(isDisplayStr);
			
			NoticeItem item = new NoticeItem();
			item.setNoticeTitle(noticeTitle);
			item.setNoticeText(noticeText);
			item.setIdNotice(idNotice);
			item.setIsDisplay(isDisplay);
			
			int page = Integer.parseInt(pageStr);
			
			HttpSession session = request.getSession();
			AdminItem loginInfo = (AdminItem) session.getAttribute("ADMIN_INFO");
			item.setIdAdmin(loginInfo.getIdAdmin());
			
			int result = noticeDao.editNotice(item);
			
			if (result > 0) {
				try {
					response.sendRedirect("listNotice?page=" + page);
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				Util.getInstance().alertPage("수정에 실패하였습니다.", response);
			}
		}
		return null;
	}
	
	/** 공지사항 삭제후 리스트 목록 */
	@RequestMapping(value = "deleteNoticeOk", method = RequestMethod.POST)
	public String deleteNoticeOk(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		String idNoticeStr = request.getParameter("idNotice");
		String pageStr = request.getParameter("page");
		
		if (idNoticeStr == null || pageStr == null || idNoticeStr.equals("") || pageStr.equals("")) {
			Util.getInstance().alertPage("필수입력 항목이 누락되었습니다.", response);
		} else {
			int idNotice = Integer.parseInt(idNoticeStr);
			
			NoticeItem item = new NoticeItem();
			item.setIdNotice(idNotice);
			
			int page = Integer.parseInt(pageStr);
			
			int result = noticeDao.deleteNotice(item);
			
			if (result > 0) {
				try {
					response.sendRedirect("listNotice?page=" + page);
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				Util.getInstance().alertPage("삭제에 실패하였습니다.", response);
			}
		}
		return null;
	}

}